﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Common.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class AbstractWorker
    {
        private string _AssemblyFileName;
        /// <summary></summary>
        public string AssemblyFileName
        {
            get
            {
                return this._AssemblyFileName;
            }
            set
            {
                this._AssemblyFileName = value;
            }
        }

        private string _TypeFullName;
        /// <summary></summary>
        public string TypeFullName
        {
            get
            {
                return this._TypeFullName;
            }
            set
            {
                this._TypeFullName = value;
            }
        }

        /// <summary>
        /// assembly file name must exist in bin folder
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="typeFullName"></param>
        public AbstractWorker(string assemblyName, string typeFullName)
        {
            this._AssemblyFileName = assemblyName;
            this._TypeFullName = typeFullName;
        }

        /// <summary>
        /// the type must have an empty constructor
        /// </summary>
        /// <returns></returns>
        public object Instantiate()
        {
            string binFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string assemblyFilePath = Path.Combine(binFolderPath, this._AssemblyFileName);
            Assembly assembly = Assembly.LoadFrom(assemblyFilePath);
            Type type = assembly.GetType(this._TypeFullName);
            return Activator.CreateInstance(type);
        }
    }
}
